package com.hmyuan.controller.back;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hmyuan.domain.Sys_resources;
import com.hmyuan.domain.Sys_role;
import com.hmyuan.domain.Sys_user;
import com.hmyuan.domain.Sys_user_roleKey;
import com.hmyuan.service.SysResourcesService;
import com.hmyuan.service.SysRoleService;
import com.hmyuan.service.SysUserService;

/**
 * 系统用户controller
 * @author hmyuan
 *
 */
@Controller
@RequestMapping("/back/sysuser")
public class SysUserController {
	
	@Resource
	SysUserService sysUserService;
	@Resource
	SysRoleService sysRoleService;
	@Resource
	SysResourcesService sysResourcesService;
	/**
	 * 新增用户页面提交
	 */
	@ResponseBody
	@RequestMapping("/addSysuser")
	public String addSysuser(HttpServletRequest request,Model model,Sys_user sys_user){
		sys_user.setCreatetime(new Date());
		sys_user.setCredentialssalt(new Random(System.currentTimeMillis()).nextLong()+"");
		sys_user.setDeletestatus(0);
		sys_user.setIsadmin(0);
		sys_user.setStatus("0");

		sysUserService.insert(sys_user);
		return "success";
	}
	/**
	 * 编辑用户页面提交
	 */
	@ResponseBody
	@RequestMapping("/updateSysuser")
	public String updateSysuser(HttpServletRequest request,Model model,Sys_user sys_user){
		if(null==sys_user.getId()||"sysadmin".equals(sys_user.getAccountname())) return "fail";
		sysUserService.updateByPrimaryKeySelective(sys_user);
		return "success";
	}
	/**
	 * 启用和禁用用户
	 */
	@ResponseBody
	@RequestMapping("/sysuserUpadateStatus")
	public String sysuserUpadateStatus(HttpServletRequest request,Model model,Long[] ids,Integer status){
		if(null==status||null==ids||ids.length==0) return "fail";
		Sys_user sys_user = new Sys_user();
		sys_user.setIds(ids);
		sys_user.setStatus(status+"");//修改状态
		sysUserService.updateByPrimaryKeySelective(sys_user);
		return "success";
	}
	/**
	 * 删除用户
	 */
	@ResponseBody
	@RequestMapping("/sysuserDelete")
	public String sysuserDelete(HttpServletRequest request,Model model,Long[] ids){
		if(null==ids||ids.length==0) return "fail";
		Sys_user sys_user = new Sys_user();
		sys_user.setIds(ids);
		sys_user.setDeletestatus(1);//状态置为删除
		sysUserService.updateByPrimaryKeySelective(sys_user);
		return "success";
	}
	/**
	 * 修改用户信息页面
	 */
	@RequestMapping("/sysuserUpdatePage")
	public String sysuserUpdatePage(HttpServletRequest request,Model model,Long id){
		if(null==id) return "/sysuser/sysuserAddPage";
		Sys_user sys_user = sysUserService.selectByPrimaryKey(id);
		request.setAttribute("sys_user", sys_user);
		return "/back/sysuser/sysuserUpdatePage";
	}
	/**
	 * 当前用户信息页面
	 */
	@RequestMapping("/currentuserPage")
	public String currentuserPage(HttpServletRequest request,Model model){
		Sys_user currentuser = (Sys_user) request.getSession().getAttribute("currentuser");
		request.setAttribute("sys_user", currentuser);
		return "/back/sysuser/currentuserPage";
	}
	/**
	 * 编辑当前用户页面提交
	 */
	@ResponseBody
	@RequestMapping("/updateCurrentuser")
	public String updateCurrentuser(HttpServletRequest request,Model model,Sys_user sys_user){
		if("sysadmin".equals(sys_user.getAccountname())) return "sysadmin";
		sys_user.setId(((Sys_user)request.getSession().getAttribute("currentuser")).getId()); 
		sys_user.setPassword(null);
		sysUserService.updateByPrimaryKeySelective(sys_user);
		return "success";
	}
	/**
	 * 修改当前用户密码提交
	 */
	@ResponseBody
	@RequestMapping("/updateCurrentuserPassword")
	public String updateCurrentuserPassword(HttpServletRequest request,Model model,String oldpassword,String newpassword){
		Sys_user currentuser = (Sys_user)request.getSession().getAttribute("currentuser");
		if(null==newpassword||newpassword.length()<6||null==oldpassword||!oldpassword.equals(currentuser.getPassword()))return "fail";
		Sys_user sys_user = new Sys_user();
		sys_user.setId(currentuser.getId()); 
		sys_user.setPassword(newpassword);
		sysUserService.updateByPrimaryKeySelective(sys_user);
		return "success";
	}
	/**
	 * 检查用户账号是否可用
	 */
	@ResponseBody
	@RequestMapping("/sysuserCheckAccountname")
	public String sysuserCheckAccountname(HttpServletRequest request,Model model,String accountname,Long id){
		if(null==accountname||"".equals(accountname) ) return "fail";
		Sys_user sys_user = new Sys_user();
		sys_user.setAccountname(accountname);
		List<Sys_user> list = sysUserService.selectList(sys_user);
		if(null==list||list.size()==0||(id!=null&&list.size()==1&&list.get(0).getId()==id)){
			return "success";
		}else{
			return "fail";
		}
	}
	/**
	 * 分页查询用户列表
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/findSysuserList")
	public String findSysuserList(HttpServletRequest request,Model model,Integer pageNum,Sys_user sys_user){
		if(pageNum==null) pageNum=1;
		request.setAttribute("pageInfo", sysUserService.selectListByPage(sys_user, pageNum, 15));
		return "/back/sysuser/sysuserPageList";
	}
	/**
	 * 用户管理页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/sysuserPage")
	public String sysuserPage(HttpServletRequest request,Model model){
		return "/back/sysuser/sysuserPage";
	}
	/**
	 * 用户新增页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/sysuserAddPage")
	public String sysuserAddPage(HttpServletRequest request,Model model){
		return "/back/sysuser/sysuserAddPage";
	}
	/**
	 * 用户角色分配页面
	 */
	@RequestMapping("/sysuserRoleUpdatePage")
	public String sysuserRolePage(HttpServletRequest request,Model model,Long userid){
		
		request.setAttribute("userid", userid);
		
		return "/back/sysuser/sysuserRoleUpdatePage";
	}
	/**
	 * 用户资源权限分配页面
	 */
	@RequestMapping("/sysuserResourcesUpdatePage")
	public String sysuserResourcesUpdatePage(HttpServletRequest request,Model model,Long userid){
		
		Sys_resources sys_resources = new Sys_resources();
		sys_resources.setIshide(0);//查询未被隐藏的数据
		List<Sys_resources> list = sysResourcesService.selectList(sys_resources);
		List<Sys_resources> userreslist = sysResourcesService.selectListByUserid(userid);
		//将菜单遍历为树形结构
		List<Sys_resources> sysList = new ArrayList<>();
		for (Sys_resources sys : list) {
			for (Sys_resources sys2 : list) {
				if(sys.getParentid()!=null&&sys.getParentid()==sys2.getId()){
					if(sys2.getNodes()==null){sys2.setNodes(new ArrayList<Sys_resources>());}
					sys2.getNodes().add(sys);
				}
			}
			if(null!=sys.getLevel()&&sys.getLevel()==1){//判断是否为主目录
				sysList.add(sys);
			}
		}
		request.setAttribute("sysList", sysList);
		request.setAttribute("userid", userid);
		request.setAttribute("userreslist", userreslist);
		
		return "/back/sysuser/sysuserResourcesUpdatePage";
	}
	/**
	 * 用户资源权限分配提交
	 */
	@ResponseBody
	@RequestMapping("/sysuserResourcesUpdate")
	public String sysuserResourcesUpdate(HttpServletRequest request,Model model,Long[] resIds,Long userid){
		if(null==userid) return "fail";
		sysUserService.insertResUsers(userid, resIds);
		return "success";
	}
	/**
	 * 用户角色列表
	 */
	@RequestMapping("/sysuserRoleUpdatePageList")
	public String sysuserRoleUpdatePageList(HttpServletRequest request,Model model,Long userid){
		
		request.setAttribute("sysuserRoleList", userid==null?null:sysRoleService.selectListByUserid(userid));
		
		return "/back/sysuser/sysuserRoleUpdatePageList";
	}
	/**
	 * 给用户添加角色
	 */
	@ResponseBody
	@RequestMapping("/sysuserRoleAdd")
	public String sysuserRoleAdd(HttpServletRequest request,Model model,Sys_user_roleKey sys_user_role){
		
		sysUserService.insertUserRole(sys_user_role);
		return "success";
	}
	/**
	 * 移除用户角色
	 */
	@ResponseBody
	@RequestMapping("/sysuserRoleRemove")
	public String sysuserRoleRemove(HttpServletRequest request,Model model,Sys_user_roleKey sys_user_role){
		if(sys_user_role.getRoleid()==null||sys_user_role.getUserid()==null){
			return "fail";
		}
		sysUserService.deleteUserRole(sys_user_role);
		return "success";
	}
	/**
	 * 分页查询所有角色列表
	 */
	@RequestMapping("/findSysroleSelectList")
	public String findSysroleSelectList(HttpServletRequest request,Model model,Integer pageNum,Sys_role sys_role){
		if(pageNum==null) pageNum=1;
		sys_role.setState("0");
		request.setAttribute("pageInfo", sysRoleService.selectListByPage(sys_role, pageNum, 10));
		return "/back/sysuser/sysroleSelectList";
	}
}
